﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_QuanLyTinTuc : System.Web.UI.Page
{
    TinTucContronller TinTucCTR = new TinTucContronller();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Employee"] == null)
             Response.Redirect("login.aspx");
        if (!IsPostBack)
        {
            if (Request["tintuctk"] == null)
                gvDanhSachTin.DataSource = TinTucCTR.fnC_GetAllDataTinTuc();
            else
            {
                hdtimkiem.Value = Request["tintuctk"].ToString();
                string timkiem = "%" + hdtimkiem.Value + "%";
                gvDanhSachTin.DataSource = TinTucCTR.fnC_TimKiemTintuc(timkiem);
            }
            
            DataBind();
        }
    }
    protected void gvDanhSachTin_SelectedIndexChanged(object sender, EventArgs e)
    {
        HiddenField idkm = (HiddenField)gvDanhSachTin.SelectedRow.Cells[7].FindControl("hdidkm");
        Response.Redirect("capnhattintuc.aspx?update=true&&idkm=" + idkm.Value);
    }
    protected void gvDanhSachTin_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDanhSachTin.PageIndex = e.NewPageIndex;
        if (hdtimkiem.Value == "")
            gvDanhSachTin.DataSource = TinTucCTR.fnC_GetAllDataTinTuc();
        else
        {
            string timkiem = "%" + hdtimkiem.Value + "%";
            gvDanhSachTin.DataSource = TinTucCTR.fnC_TimKiemTintuc(timkiem);
        }
       
        DataBind();
    }
    protected void gvDanhSachTin_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = (ImageButton)e.Row.Cells[6].Controls[0];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "if(!confirm('Bạn có muốn xóa tin :<" + Server.HtmlDecode(e.Row.Cells[1].Text) + ">  không?'))return false";
        }
    }
    protected void gvDanhSachTin_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        TinTucKhuyenMaiEntity TinTucE = new TinTucKhuyenMaiEntity();
        HiddenField idkm = (HiddenField)gvDanhSachTin.Rows[e.RowIndex].Cells[7].FindControl("hdidkm");
        TinTucE.Idkm = int.Parse(idkm.Value);
        TinTucE = TinTucCTR.fnG_GetTinTuc(idkm.Value);
        TinTucCTR.fnC_DeleteTinTuc(TinTucE);
        File.Delete(Server.MapPath("~/images/TinTuc/") + TinTucE.HinhAnhDaiDien+ ".jpg");
        gvDanhSachTin.DataSource = TinTucCTR.fnC_GetAllDataTinTuc();
        DataBind();
    }
    protected void ImagebtnActive_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton imbtactive = (ImageButton)sender;
        TinTucKhuyenMaiEntity TinTucE = new TinTucKhuyenMaiEntity();
        TinTucE.Idkm = int.Parse(imbtactive.CommandArgument);
        TinTucCTR.fnC_ActiveTinTuc(TinTucE);
    }
    protected void imgbtsearch_Click(object sender, ImageClickEventArgs e)
    {
        DateTime tungay = DateTime.Parse(txttungay.Text);
        DateTime denngay = DateTime.Parse(txtdenngay.Text);
        gvDanhSachTin.DataSource = TinTucCTR.fnC_GetDataForDate(tungay, denngay);
        DataBind();
    }
}